// =======================================================================
// 
//       Filename:  main.cpp
// 
//    Description:  
// 
//        Version:  1.0
//        Created:  27.03.2010 17:15:48
//       Revision:  none
//       Compiler:  g++
// 
//         Author:  Dominik 'domachine' Burgdörfer (-), dominik.burgdoerfer@googlemail.com
//        Company:  -
// 
// =======================================================================

#include "plodder.hpp"
#include <fstream>
#include <cstdlib>
#include <boost/program_options.hpp>

namespace po = boost::program_options;

int main(int ac, char** av) {
    std::string str;
    std::map<std::string,std::string>
        config;

    std::string configPath(getenv("HOME"));
    configPath.append("/.webplodder");

    webplodder::Plodder plodder(configPath);

    std::vector<std::string> inputDirectories;

    try {
        po::options_description desc("Options");
        po::positional_options_description pos;

        desc.add_options()
            ("help,h", "produce help message")
            ("cache,c", po::value<std::string>(),
                    "specify cache mode. Valid ones are:\n"
                    "\tnon, script, all")
            ("input-dir", po::value<std::vector<std::string> >(),
             "add input directory")
            ;

        pos.add("input-dir", -1);

        po::command_line_parser parser(ac, av);
        parser = parser.options(desc).positional(pos);

        po::variables_map vm;
        po::store(parser.run(), vm);

        po::notify(vm);

        if(vm.count("help")) {
            std::cout << desc << std::endl;
            return 0;
        }

        if(vm.count("cache-mode")) {
            std::string value = vm["cache-mode"].as<std::string>();

            if(value == "non")
                plodder.setCacheMode(webplodder::Parser::CACHE_NOTHING);
            else if(value == "script")
                plodder.setCacheMode(webplodder::Parser::CACHE_SCRIPT);
            else if(value == "all")
                plodder.setCacheMode(webplodder::Parser::CACHE_ALL);
            else {
                std::cerr << "error: unknown cache mode: "
                          << value << std::endl;
                return 1;
            }
        }

        if(vm.count("input-dir")) {
            inputDirectories = 
                vm["input-dir"].as<std::vector<std::string> >();
        }
        else {
            std::cerr << "error: no input directories" << std::endl;
            return 1;
        }
    }
    catch(std::exception& e) {
        std::cerr << "error: " << e.what() << "\n";
        return 1;
    }

    try {
        std::vector<std::string>::iterator iter = 
            inputDirectories.begin();
        plodder.init();

        for(; iter != inputDirectories.end(); ++iter) {
            plodder.scanDir(*iter);
        }
    }
    catch(YAML::Exception& e) {
        std::cerr << "error while reading config file:\n"
                  << " -> " << e.what() << std::endl;
    }
    catch(std::runtime_error& e) {
        std::cerr << "error: " << e.what() << std::endl;
    }

    return 0;
}
